package com.jinke.uang.view;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.jinke.uang.R;
import com.jinke.uang.bean.OrderItemData;
import com.jinke.uang.common.RouterMap;
import com.jinke.uang.common.DialogManager;
import com.jinke.uang.constant.Constant;
import com.jinke.uang.utils.PXUtils;
import com.jinke.uang.utils.StringUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class RepaymentDialog extends Dialog {
    private TextView tv_money;
    private TextView tv_status;
    private Button btn_repay;
    private Button btn_delay;
    private TextView tv_repayment_date;
    private Context context;
    private int status;

    public RepaymentDialog(@NonNull Context context) {
        super(context);
        init(context);
    }

    public RepaymentDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        init(context);
    }

    protected RepaymentDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }


    public void init(Context context) {
        this.context = context;

        View view = LayoutInflater.from(context).inflate(R.layout.view_repayment_dialog, null);
        tv_money = view.findViewById(R.id.tv_money);
        tv_status = view.findViewById(R.id.tv_status);
        btn_repay = view.findViewById(R.id.btn_repay);
        btn_delay = view.findViewById(R.id.btn_delay);
        tv_repayment_date = view.findViewById(R.id.tv_repayment_date);

        setContentView(view);
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);
        window.getDecorView().setPadding(0, 0, 0, 0);
        window.setLayout(PXUtils.dp2px(getContext(), 320), WindowManager.LayoutParams.WRAP_CONTENT);//设置横向全屏

        setCancelable(false);
        setCanceledOnTouchOutside(false);
    }


    @SuppressLint("SetTextI18n")
    public void setContent(final OrderItemData orderItemData) {
        tv_money.setText(context.getResources().getString(R.string.money_unit_hint) + " "
                + StringUtils.getDotMoneyStr(String.valueOf(orderItemData.getRepayAmt())));
        tv_repayment_date.setText(orderItemData.getLastPayDate());
        setStatus(orderItemData.getStatus());
        btn_repay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String title;
                if (status == Constant.ORDER_STATUS_OVERDUE) {
                    title = context.getResources().getString(R.string.borrow_detail_page_title_overdue);
                } else {
                    title = context.getResources().getString(R.string.borrow_detail_page_title);
                }
                ARouter.getInstance().build(RouterMap.MINE_BROWING_DETAIL)
                        .withString(Constant.PAGE_TITLE, title)
                        .withSerializable(Constant.BORROW_DETAIL, orderItemData).navigation();
                DialogManager.getManager().dismissDialog(RepaymentDialog.this);
            }
        });
        btn_delay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogManager.getManager().dismissDialog(RepaymentDialog.this);
            }
        });
    }

    public void setStatus(int status) {
        this.status = status;
        switch (status) {
            case Constant.ORDER_STATUS_LENDING:
                tv_status.setText(context.getResources().getString(R.string.repayment_type_lending));
                break;
            case Constant.ORDER_STATUS_OVERDUE:
                tv_status.setText(context.getResources().getString(R.string.repayment_type_overdue));
                break;
            case Constant.ORDER_STATUS_ROLLOVER:
                tv_status.setText(context.getResources().getString(R.string.repayment_type_rollover));
                break;
            default:
                tv_status.setVisibility(View.GONE);
                break;
        }
    }


}
